package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbPrefMapper;
import com.guigu.qingcheng.pojo.TbPref;
import com.guigu.qingcheng.service.TbPrefService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbPrefServiceImpl implements TbPrefService {
    @Autowired
    private TbPrefMapper tbPrefMapper;



    @Override
    public List<TbPref> queryAll() {
        List<TbPref> tbPref = tbPrefMapper.selectAll();
            return tbPref;
    }

    @Override
    public PageUtil<TbPref> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbPref> pages = (Page<TbPref>) tbPrefMapper.selectAll();
        PageUtil<TbPref> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbPref> pageQueryByAll(Integer pageNum, Integer pageSize, TbPref tbPref) {

        Example example=new Example(TbPref.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name",brand.getName());
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbPref> pages = (Page<TbPref>) tbPrefMapper.selectByExample(example);
        PageUtil<TbPref> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbPref(TbPref tbPref) {
        int insert = tbPrefMapper.insert(tbPref);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbPref(TbPref tbPref) {
        int i = tbPrefMapper.updateByPrimaryKeySelective(tbPref);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbPrefMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbPref findById(Integer id) {
        TbPref tbPref = tbPrefMapper.selectByPrimaryKey(id);
        return tbPref;
    }
}
